package com.hyg.oa.personnel.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hyg.oa.common.DateUtil;
import com.hyg.oa.common.PageUtils;
import com.hyg.oa.common.Pagination;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hyg.oa.common.UUIDUtils;
import com.hyg.oa.personnel.model.Personnel;
import com.hyg.oa.personnel.service.PersonnelService;


/**
 * Controller类
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/personnel") 
public class PersonnelController {
	
	@Autowired
	private PersonnelService personnelService;
	
	/**
	 * page
	 * @return
	 */
	@RequestMapping("/page")
	public ModelAndView page(HttpServletRequest request) {
		Map<String, Object> model = new HashMap<String,Object>();
		
		//总数
		long totalCount = this.personnelService.findTotalCount();
		//page对象
		Pagination pagination = PageUtils.create(request, totalCount);
		
		//数据
		List<Personnel> list = this.personnelService.findPage(pagination.getPageNo(), pagination.getPageSize());
		model.put("list", list);
		model.put("pagination", pagination);
		
		return new ModelAndView("/jsp/personnel/list.jsp", model);
	}
	
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list() {
		Map<String, Object> model = new HashMap<String,Object>();
		
		List<Personnel> list = this.personnelService.findAll();
		model.put("list", list);
		
		return new ModelAndView("/jsp/personnel/list.jsp", model);
	}
	
	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add() {
		return new ModelAndView("/jsp/personnel/add.jsp");
	}
	
	
	
	/**
	 * 保存
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public Object save(HttpServletRequest request) {
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		try {
			String name = request.getParameter("name");
			String department = request.getParameter("department");
			String number = request.getParameter("number");
			String birthday = request.getParameter("birthday");
			String gender = request.getParameter("gender");
			String nation = request.getParameter("nation");
			String position = request.getParameter("position");
			String level = request.getParameter("level");
			String type = request.getParameter("type");
			String phone = request.getParameter("phone");
			String remark = request.getParameter("remark");
		
			Personnel personnel = new Personnel();
			personnel.setName(name);
			personnel.setDepartment(department);
			personnel.setNumber(number);
			personnel.setBirthday(DateUtil.toDate(birthday, "yyyy-MM-dd"));
			personnel.setGender(gender);
			personnel.setNation(nation);
			personnel.setPosition(position);
			personnel.setLevel(level);
			personnel.setType(type);
			personnel.setPhone(phone);
			personnel.setRemark(remark);
			personnel.setCreatetime(new Date());
			personnel.setId(UUIDUtils.generate());
			this.personnelService.save(personnel);
			
			result.put("success", true);
			
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		
		return result;
	}
	
	
	/**
	 * 用户编辑
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(HttpServletRequest request) {
		Map<String, Object> model = new HashMap<String,Object>();
		
		String id = request.getParameter("id");
		Personnel personnel = this.personnelService.findById(id);
		model.put("personnel", personnel);
		
		return new ModelAndView("/jsp/personnel/edit.jsp", model);
	}
	
	
	/**
	 * 更新
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Object update(HttpServletRequest request) {
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		try {
			String id = request.getParameter("id");
			String name = request.getParameter("name");
			String department = request.getParameter("department");
			String number = request.getParameter("number");
			String birthday = request.getParameter("birthday");
			String gender = request.getParameter("gender");
			String nation = request.getParameter("nation");
			String position = request.getParameter("position");
			String level = request.getParameter("level");
			String type = request.getParameter("type");
			String phone = request.getParameter("phone");
			String remark = request.getParameter("remark");
			
			Personnel personnel = this.personnelService.findById(id);
			personnel.setName(name);
			personnel.setDepartment(department);
			personnel.setNumber(number);
			personnel.setBirthday(DateUtil.toDate(birthday, "yyyy-MM-dd"));
			personnel.setGender(gender);
			personnel.setNation(nation);
			personnel.setPosition(position);
			personnel.setLevel(level);
			personnel.setType(type);
			personnel.setPhone(phone);
			personnel.setRemark(remark);
			personnel.setUpdatetime(new Date());
			this.personnelService.update(personnel);
			
			result.put("success", true);
			
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		
		return result;
	}

	
	/**
	 * 删除
	 * @return
	 */
	@RequestMapping("/del")
	@ResponseBody
	public Object del(HttpServletRequest request) {
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		try {
			String id = request.getParameter("id");
			this.personnelService.delete(id);
			
			result.put("success", true);
			
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		
		return result;
	}
}
